package com.leyou.search.controller;

import com.leyou.common.domain.PageResult;
import com.leyou.common.enums.ItemExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.dto.CategoryDto;
import com.leyou.search.doc.Good;
import com.leyou.search.dto.SearchRequest;
import com.leyou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @author: Ljxpa
 * @date: 2020/03/12 16:58
 * @description:
 */
@RestController
public class SearchController {

	@Autowired
	private SearchService searchService;

	/**
	 * 分页搜索
	 * @param searchRequest 包含搜索的关键字，页数的vo对象
	 * @return 从索引库中搜索到的good的list集合
	 */
	@PostMapping("/page")
	public ResponseEntity<PageResult<Good>> pageQueryGoods(@RequestBody SearchRequest searchRequest) {
		PageResult<Good> goodPageResult = searchService.pageQueryGoods(searchRequest);
		return ResponseEntity.ok(goodPageResult);
	}

	/**
	 * 根据用户输入的搜索内容，提供用户可供过滤的选项。前端展示的时候参考京东，分析后得出给予前端一个map集合，
	 * map的key就是可供用户选择过滤的大类，value是实际可以进行过滤的选项
	 *
	 * @param searchRequest 用户输入的搜索内容
	 */
	@PostMapping("/filterList")
	public ResponseEntity<Map<String, List<?>>> queryFilterListByKey(@RequestBody SearchRequest searchRequest) {
		if (null != searchRequest) {
			Map<String, List<?>> map = searchService.queryFilterListByKey(searchRequest);
			return ResponseEntity.ok(map);
		}else {
			throw new LyException(ItemExceptionEnum.DATA_NOT_FOUND);
		}
	}

	@GetMapping("/categories")
	public ResponseEntity<List<CategoryDto>> queryCategoriesByCid3(@RequestParam("cid3")Long cid3){
		List<CategoryDto> categoryDtoList = searchService.queryCategoriesByCid3(cid3);
		return ResponseEntity.ok(categoryDtoList);
	}

}
